//
//  UINavigationItem+CustomBarButtonItem.m
//  CustomNavigationBar
//
//  Created by zkingsoft on 15/12/29.
//  Copyright (c) 2015年 zkingsoft. All rights reserved.
//

#import "UINavigationItem+CustomBarButtonItem.h"

#import "NavigationBarButtonItem.h"

@implementation UINavigationItem (CustomBarButtonItem)


// 使用title 初始化 title
- (void)setNewTitle:(NSString *)title
{
    UILabel *label = [[UILabel alloc] init];
    label.frame = CGRectMake(0, 0, 180, 20);
    label.backgroundColor = [UIColor clearColor];
    label.font = [UIFont systemFontOfSize:18];
    label.textColor = [UIColor whiteColor];
    label.textAlignment = NSTextAlignmentCenter;
    label.text = title;
    self.titleView = label;
}

// 使用image初始化titile
- (void)setNewTitleWithImage:(UIImage *)image
{
    UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
    CGRect bounds = imageView.bounds;
    bounds.size  =  image.size;
    imageView.bounds = bounds;
    self.titleView = imageView;
}


// set 设置left button 并可以添加事件
- (void)setLeftItemWithTarget:(id)target action:(SEL)action title:(NSString *)title{
    NavigationBarButtonItem *buttonItem = [NavigationBarButtonItem defauleItemWithTarget:target action:action title:title];
    
    self.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:buttonItem.button];
}

- (void)setLeftItemWithTarget:(id)target action:(SEL)action image:(NSString *)image{
    NavigationBarButtonItem *buttonItem = [NavigationBarButtonItem defauleItemWithTarget:target action:action image:image];
    
    self.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:buttonItem.button];
}



// set 设置right button 并可以添加事件
- (void)setRightItemWithTarget:(id)target action:(SEL)action title:(NSString *)title{
    NavigationBarButtonItem *buttonItem = [NavigationBarButtonItem defauleItemWithTarget:target action:action title:title];
    
    self.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:buttonItem.button];
}

- (void)setRightItemWithTarget:(id)target action:(SEL)action image:(NSString *)image{
    NavigationBarButtonItem *buttonItem = [NavigationBarButtonItem defauleItemWithTarget:target action:action image:image];
    
    self.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:buttonItem.button];
}



// set 设置返回的 button 并添加事件
- (void)setBackItemWithTarget:(id)target action:(SEL)action{
    NavigationBarButtonItem *backButtonItem = [NavigationBarButtonItem backItemWithTarget:target action:action title:@"返回"];
    
    self.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backButtonItem.button];
}

- (void)setBackItemWithTarget:(id)target action:(SEL)action title:(NSString *)title{
    NavigationBarButtonItem *backButtonItem = [NavigationBarButtonItem backItemWithTarget:target action:action title:title];
    
    self.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backButtonItem.button];
}

@end









